package com.shu;

import lombok.Data;
import lombok.extern.java.Log;

import javax.sound.midi.Soundbank;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.shu
 * @类名称：ThreldLocal
 * @创建人：YIHANG.XU
 * @创建时间：2020/10/115:50 下午
 * @修改人：YIHANG.XU
 * @修改时间：2020/10/115:50 下午
 * @修改备注：
 * @Copyright
 */
@Data
public class ThreldLocalDemoRun {
    static ThreadLocal<String> localVariable = new ThreadLocal<>();

    static void print(String str) {
        System.out.println(str + "local Value  " + localVariable.get());

        localVariable.remove();


    }

    /**
     * ThreadLocal 是个个线程私有的线程局部变量表，每个线程私有的
     * @param args
     */

    public static void main(String[] args) {
        Thread threadOne = new Thread(() -> {
            localVariable.set("这是线程1");
            print("线程1");
            System.out.println("线程1" + localVariable.get());
        });
        Thread threadTwe = new Thread(() -> {
            localVariable.set("线程2");
            print("线程2");
            System.out.println("线程2" + localVariable.get());
        });

        threadOne.start();
        threadTwe.start();
    }

}

/**
 * InheritableThreadLocal 你可以获取到父线程线程标量表的数据
 */
class ThreldLocalDemoRunTwo {
    public static void main(String[] args) {
        InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
        ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();
        stringThreadLocal.set("keishi");
        inheritableThreadLocal.set("sss");
        new Thread(() -> {
            String s = inheritableThreadLocal.get();
            System.out.println(s);
            String s1 = stringThreadLocal.get();
            System.out.println(s1);
        }).start();

    }
}
